package com.wondertek.service.service.business.impl;

import com.wondertek.service.dto.business.res.*;
import com.wondertek.service.mapper.business.WebHomePageMapper;
import com.wondertek.service.service.business.WebHomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyu
 */
@Service
public class WebHomePageServiceImpl implements WebHomePageService {

    @Autowired
    private WebHomePageMapper webHomePageMapper;

    @Override
    public WhpTopRes findTop() {
        return webHomePageMapper.findTop();
    }

    @Override
    public WhpMapSiteRes findMapSiteBySiteId(String siteId) {
        return webHomePageMapper.findMapSiteBySiteId(siteId);
    }

    @Override
    public List<WhpRightRes> findRight() {
        List<WhpRightRes> right = webHomePageMapper.findRight();

        int sum = 0;
        int other = 0;

        for (int i = 0; i < right.size(); i++) {
            WhpRightRes whpRightRes = right.get(i);
            sum += whpRightRes.getSum();

            if(i >= 5)
                other += whpRightRes.getSum();
        }

        List<WhpRightRes> res = new ArrayList<>();

        for (int i = 0; i < right.size(); i++) {
            if(i == 5){
                WhpRightRes whpRightRes = new WhpRightRes();
                whpRightRes.setDeviceType("Others");
                whpRightRes.setSum(other);
                whpRightRes.setRatio((int) Math.round((double) other / sum * 100));
                res.add(whpRightRes);
                break;
            }

            WhpRightRes whpRightRes = right.get(i);
            whpRightRes.setRatio((int) Math.round((double) whpRightRes.getSum() / sum * 100));
            res.add(whpRightRes);

        }

        return res;
    }

    @Override
    public WhpBottomLeftRes findBottomLeft() {
        return webHomePageMapper.findBottomLeft();
    }

    @Override
    public List<WhpBottomCentreRes> findBottomCentre() {
        return webHomePageMapper.findBottomCentre();
    }

    @Override
    public WhpBottomRightRes findBottomRight() {
        WhpBottomRightRes bottomRight = webHomePageMapper.findBottomRight();

        bottomRight.setRatio((int) Math.round((double) bottomRight.getOffLineSum() / bottomRight.getSum() * 100));

        return bottomRight;
    }
}
